Public Partial Class FrmListadoFacturaCompra
    Inherits System.Web.UI.Page
    Public Factura As CapaDeNegocios.nFacturaCompra = New CapaDeNegocios.nFacturaCompra
    Public Proveedor As CapaDeNegocios.nProveedor = New CapaDeNegocios.nProveedor
    Public Condicion As CapaDeNegocios.nCondicion = New CapaDeNegocios.nCondicion
    Public Remito As CapaDeNegocios.nRemito = New CapaDeNegocios.nRemito
    Dim IdFactura As String
    Dim IdBoton As String
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Session("Login") = "Anonimo" Then
            Response.Redirect("/")
        End If
        If Not Page.IsPostBack Then
            IdFactura = Request.QueryString("ID")
            IdBoton = Request.QueryString("IDBoton")
            LLenarGridRemitos()
            '            IdRemito = Request.QueryString("ID")
            LLenarComboCondicion()
            LLenarcomboProv()
            LblError.Visible = False
            BtnImprimir.Visible = False

            BtnImprimir.OnClientClick = "javascript:var popup = window.open('../Reportes/RptListadoDeFacturasDeCompra.aspx',(new Date().getTime()),'Width=900,Height=500,Scrollbars=NO,Resizable=NO,Location=NO,Menubar=NO,Status=NO,Titlebar=NO,Toolbar=NO')"

        End If
    End Sub
    Sub LLenarGridRemitos()
        Dim Estado As String
        Dim Condicion As String
        Dim Prov As Integer
        Dim Numero As String
        If CboEstado.SelectedItem.ToString() = "Cancelado" Then
            Estado = ""
        Else
            Estado = CboEstado.SelectedItem.ToString()
        End If
        If CboCondicion.SelectedItem.ToString() = "Ninguno" Then
            Condicion = ""
        Else
            Condicion = CboCondicion.SelectedValue.ToString()
        End If
        If CboProveedor.SelectedValue.ToString() = -1 Then
            Prov = 0
        Else
            Prov = CboProveedor.SelectedValue.ToString()
        End If
        If TxtNumero.Text = "" Then
            NUmero = ""
        Else
            Numero = TxtNumero.Text
        End If
        GridFacturas.DataSource = Factura.ListarFacturasCompletas(0, Int(Prov), Numero, Estado, 0, 0, Condicion, "")
        GridFacturas.DataBind()
        BtnImprimir.Visible = True
        Session("RptListadoDeFacturasDeCompra") = GridFacturas.DataSource

    End Sub
    Sub LimpiarFormulario()
        TxtNumero.Text = ""
        CboCondicion.SelectedIndex = -1
        CboProveedor.SelectedIndex = -1
        CboEstado.SelectedIndex = -1
        GridFacturas.SelectedIndex = -1
        LblError.Visible = False
        LblError.Text = ""
        LLenarGridRemitos()
    End Sub
    Sub LLenarComboCondicion()
        CboCondicion.DataSource = Condicion.Listar(0, "", 0, "Compra")
        CboCondicion.DataTextField = "Descripcion"
        CboCondicion.DataValueField = "Codigocondicion"
        CboCondicion.DataBind()
    End Sub
    Sub LLenarcomboProv()
        CboProveedor.DataSource = Proveedor.Listar(0, "", "", "", "", "", 1, "")
        CboProveedor.DataTextField = "RazonSocial"
        CboProveedor.DataValueField = "Codigoprov"
        CboProveedor.DataBind()
    End Sub
    Protected Sub LnkBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LnkBuscar.Click
        LLenarGridRemitos()
    End Sub

    Private Sub GridFacturas_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridFacturas.PageIndexChanging
        GridFacturas.PageIndex = e.NewPageIndex
        LLenarGridRemitos()
    End Sub
    Protected Sub GridFacturas_RowDataBound(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridFacturas.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim a As LinkButton = e.Row.FindControl("LnkMasdatos")
            Dim b As HiddenField = e.Row.FindControl("HFFactura")

            'a.OnClientClick = "javascript:var popup = window.open('FrmDetalleDeFacturaCompra.aspx?IDOrden=" + b.Value.ToString() + "',(new Date().getTime()),'Width=700,Height=500,Scrollbars=NO,Resizable=NO,Location=NO,Menubar=NO,Status=NO,Titlebar=NO,Toolbar=NO')"
            ' a.OnClientClick = "javascript:window.opener.document.getElementById('" + IdFactura + "').value = '" + b.Value.ToString() + "';window.opener.document.getElementById('" + IdBoton + "').click();window.close();"
            '            a.OnClientClick = "javascript:window.opener.document.getElementById('" + IdFactura + "').value = '" + b.Value.ToString() + "';window.opener.location.replace('FrmDetalleDeFacturaCompra.aspx');window.close();"
            a.OnClientClick = "javascript:var popup = window.open('FrmDetalleDeFacturaCompra.aspx?IDFactura=" + b.Value.ToString() + "',(new Date().getTime()),'Width=700,Height=500,Scrollbars=NO,Resizable=NO,Location=NO,Menubar=NO,Status=NO,Titlebar=NO,Toolbar=NO')"


        End If
    End Sub

    Protected Sub LinkCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkCancelar.Click
        LimpiarFormulario()
    End Sub

    Protected Sub GridFacturas_RowCommand(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridFacturas.RowCommand
        If e.CommandName = "MasDatos" Then

        End If
        If e.CommandName = "Eliminar" Then
            Dim VarRemito As Integer
            If e.CommandSource.parent.FindControl("LblEstado").text = "Pendiente" Then
                Factura.EliminarFactura(e.CommandSource.parent.parent.FindControl("HFFactura.value"))
                VarRemito = Remito.ListarRemitos(Int(e.CommandSource.parent.parent.FindControl("HFFactura.value")), "", "", 0, 0, "", "", "").Tables(0).Rows(0)("CodigoRemito").ToString()
                If VarRemito > 0 Then
                    Remito.ModificarRemito(VarRemito, "Pendiente", 0)
                End If

            Else

                LblError.Visible = True
                LblError.Text = "Error, Solo pueden anularse Facturas con estado Pendiente"
            End If
            End If
    End Sub

    Protected Sub BtnImprimir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnImprimir.Click

    End Sub
End Class